﻿using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace Puppy.Modularity
{
    public class PuppyModuleDescriptor : IPuppyModuleDescriptor
    {
        public Type Type { get; }

        public Assembly Assembly { get; }

        public IPuppyModule Instance { get; }

        public PuppyModuleDescriptor(
            [NotNull] Type type,
            [NotNull] IPuppyModule instance)
        {
            Check.NotNull(type, nameof(type));
            Check.NotNull(instance, nameof(instance));

            if (!type.GetTypeInfo().IsAssignableFrom(instance.GetType()))
            {
                throw new ArgumentException($"Given module instance ({instance.GetType().AssemblyQualifiedName}) is not an instance of given module type: {type.AssemblyQualifiedName}");
            }

            Type = type;
            Assembly = type.Assembly;
            Instance = instance;
        }

        public override string ToString()
        {
            return $"[PuppyModuleDescriptor {Type.FullName}]";
        }
    }
}
